# -*- coding: utf-8 -*-

'''eof
name:用款企业被纳入反洗钱黑名单
code:R03_EntAmlBlackListCheck_WHB_BTM
tableName:
columnName:
groups:强规则>反洗钱信息校验
dependencies:WDXT_Black,WDXT_AmlBlack
type:常用指标
datasourceType:在线指标
description:
eof'''

import sys
import pandas as pd
import json
from datetime import datetime

reload(sys)
sys.setdefaultencoding('utf-8')
null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']


def checkBlack(base, baseName, blackList):
    if base in null_type_list:
        return "获取到的数据为空"
    try:
        code = base["sysHead"]["responseCode"]
    except:
        return "接口获取失败"
    if code == "0":
        try:
            blackInfo = base.get("body").get(baseName)
        except:
            return "接口获取失败"
        if blackInfo not in null_type_list and isinstance(blackInfo, list):
            if len(blackInfo) >= 1:
                flag = False
                for black in blackInfo:
                    certId = black.get("CERTID")
                    if certId not in null_type_list and certId in blackList:
                        return 0
                    elif certId in null_type_list:
                        flag = True
                if flag:
                    return "接口数据异常"
                return 1
            else:
                return 1
        else:
            return "接口数据异常"
    else:
        return "接口获取失败"


def getRule03():
    certIdList = []
    creditCode = ratingRequest.get("creditCode")
    regNumber = ratingRequest.get("regNumber")
    extOrgCode = ratingRequest.get("extOrgCode")
    if creditCode not in null_type_list:
        certIdList.append(creditCode)
    if extOrgCode not in null_type_list:
        certIdList.append(extOrgCode)
    if regNumber not in null_type_list:
        certIdList.append(regNumber)
    if len(certIdList) < 1:
        raise RuntimeError("证件号缺失，无法判断企业黑名单")
    amlBlackResult = checkBlack(WDXT_AmlBlack, "AMLBLACKLISTDATAS", certIdList)
    if amlBlackResult == 0:
        return 0
    elif amlBlackResult != 1:
        raise RuntimeError(amlBlackResult)
    else:
        return 1


if __name__ == "__main__":
    WDXT_Black = {"sysHead": {"responseCode": "0"}, "body": {"BLACKLISTDATAS": [
        {"CERTTYPE": "Ent02", "CERTID": "5101021967061085 11"}
    ]}}
    WDXT_AmlBlack = {"sysHead": {"responseCode": "0"}, "body": {"AMLBLACKLISTDATAS": [
        {"CERTTYPE": "Ent02", "CERTID": "1234 56"}
    ]}}
    ratingRequest = {"entTaxID": "1213", "legalPerson": u"朱为广", "legalPersonIdCard": "510102196706108511",
                     "creditCode": "123456", "regNumber": "12345"}
    aa = getRule03()
    print aa

result = getRule03()
